﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JwiKIDE.Controls
{
    using JwiKIDE.ProjectManagement;

    public partial class SourceControl : UserControl
    {
        public delegate void OpenFileEventDelegate(string type, string fileName);

        public event OpenFileEventDelegate OpenFileEvent;
        public SourceControl()
        {
            InitializeComponent();
        }
        public void Update(Project project)
        {
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add("nodeSourceFiles", "SourceFiles");
            treeView1.Nodes.Add("nodeNativeFiles", "NativeFiles");
            treeView1.Nodes["nodeSourceFiles"].Nodes.Clear();
            if (project == null) return;
            foreach (string fn in project.ClassFiles)
            {
                treeView1.Nodes["nodeSourceFiles"].Nodes.Add(fn);
            }
            treeView1.Nodes["nodeSourceFiles"].Expand();
            treeView1.Nodes["nodeNativeFiles"].Nodes.Clear();
            foreach (string fn in project.NativeClassFiles)
            {
                treeView1.Nodes["nodeNativeFiles"].Nodes.Add(fn);
            }
            treeView1.Nodes["nodeNativeFiles"].Expand();
        }
        internal string GetSelectedFile()
        {
            var selectedFile = treeView1.SelectedNode;
            var parentNode = selectedFile.Parent;
            if (parentNode != null && ( parentNode.Text == "SourceFiles" || parentNode.Text == "NativeFiles"  ))
            {
                return selectedFile.Text;
            }
            return string.Empty;
        }
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode parentNode = e.Node.Parent;
            if (parentNode != null && parentNode.Name == "nodeSourceFiles")
            {
                if (OpenFileEvent != null) OpenFileEvent("class", e.Node.Text);
            }
            else if (parentNode != null && parentNode.Name == "nodeNativeFiles")
            {
                if (OpenFileEvent != null) OpenFileEvent("native", e.Node.Text);
            }
        }


    }
}
